package com.lib.controller.agent;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lib.model.enums.RentalAgentWxStatusEnum;
import com.lib.service.WxAgentService;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import me.chanjar.weixin.mp.bean.tag.WxUserTag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by duqingxiang on 17/5/18.
 */
@RestController
@RequestMapping("/agent/tool")
public class RentalAgentToolController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    WxAgentService wxService;

    @RequestMapping("/createDefaultMenu")
    public String createDefaultMenu(){

        try {
            JSONObject json = new JSONObject();

            String keyStr = "{\"type\":\""+WxConsts.XML_MSG_TEXT+"\",\"content\":\"REGISTER\"}";


            String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+wxService.getConfig().getAppid()+"&redirect_uri=http%3a%2f%2f18630342780.tunnel.2bdata.com%2fagent%2fapply_success&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";


            JSONArray buttons = new JSONArray();
            JSONObject btn1 = new  JSONObject();
            btn1.put("type","click");
            btn1.put("name","注册成为经纪人");
            btn1.put("key",keyStr);

            JSONObject btn2 = new  JSONObject();
            btn2.put("type","view");
            btn2.put("name","测试按钮23");
            btn2.put("url",url);

            buttons.add(btn1);
            buttons.add(btn2);

            json.put("button",buttons);

            String res = wxService.getMenuService().menuCreate(json.toJSONString());
            logger.info(" create default menu {}",res);
            return "执行结束";
        } catch (Exception e) {
            e.printStackTrace();
            return "出异常了呀~~~";
        }


    }


    @RequestMapping("/createNormalMenu")
    public String createNormalMenu(){

        try {
            JSONObject json = new JSONObject();

            JSONArray buttons = new JSONArray();
            JSONObject btn1 = new  JSONObject();

            String codeUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+wxService.getConfig().getAppid()+"&redirect_uri=http%3a%2f%2f18630342780.tunnel.2bdata.com%2fagent%2fmenu%2fmyCode&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";

            btn1.put("name","我的二维码");
            btn1.put("type","view");
            btn1.put("url",codeUrl);

            buttons.add(btn1);


            JSONObject btn2 = new JSONObject();
            btn2.put("name","客户分期");

            JSONArray subBtns = new JSONArray();
            JSONObject sub1 = new JSONObject();
            sub1.put("name","待处理申请");
            sub1.put("type","view");
            sub1.put("url","http://www.baidu.com");
            subBtns.add(sub1);

            JSONObject sub2 = new JSONObject();
            sub2.put("name","客户还款");
            sub2.put("type","view");
            sub2.put("url","http://www.baidu.com");
            subBtns.add(sub2);

            JSONObject sub3 = new JSONObject();
            sub3.put("name","拒批/取消");
            sub3.put("type","view");
            sub3.put("url","http://www.baidu.com");
            subBtns.add(sub3);

            btn2.put("sub_button",subBtns);

            buttons.add(btn2);
            json.put("button",buttons);

            JSONObject matchrole = new JSONObject();
            matchrole.put("tag_id", RentalAgentWxStatusEnum.NORMAL.getId());
            matchrole.put("language","zh_CN");

            json.put("matchrule",matchrole);

            String res = wxService.getMenuService().menuCreate(json.toJSONString());
            logger.info(" create applying menu {}",res);
            return "执行结束";
        } catch (Exception e) {
            e.printStackTrace();
            return "出异常了呀~~~";
        }


    }

    @RequestMapping("/createTag")
    public String createTag(){
        try {
            WxUserTag tag = wxService.getUserTagService().tagCreate("正常经纪人");
            logger.info("create agent tags {} ",tag);
            return JSON.toJSONString(tag);
        } catch (Exception e) {
            e.printStackTrace();
            return "出异常了~~~~";
        }
    }

    @RequestMapping("/test")
    public String test(){

        try{

//

            WxMpUser user = wxService.getUserService().userInfo("ohNh30icAIYuyQ3nPknRL4QRC4UI");
            logger.info("get user:: {}",user);

            boolean res = wxService.getUserTagService().batchTagging(100L,new String[]{"ohNh30icAIYuyQ3nPknRL4QRC4UI"});
            logger.info("bind tag:: {}",res);

//            WxMpQrCodeTicket ticket = wxService.getQrcodeService().qrCodeCreateTmpTicket(1,300);
//            logger.info("create qrcode  {} ",ticket);


        } catch (Exception e) {
            e.printStackTrace();
            return "出异常了拉~~~~";
        }




        return "啦啦啦啦啦~~~";
    }


}
